﻿using System.Drawing;

namespace Ycg.Utility
{
    public static class ImageHelper
    {
        public static Icon ConvertToIcon(Image image)
        {
            using (Bitmap bitmap = new Bitmap(image))
            {
                return Icon.FromHandle(bitmap.GetHicon());
            }
        }

        /// <summary>
        /// 缩放图片
        /// </summary>
        /// <param name="filePath">图片路径</param>
        /// <param name="newWidth">新宽度</param>
        /// <param name="newHeigth">新高度</param>
        /// <returns>缩放之后的图片对象</returns>
        public static Image Scaling(string filePath, int newWidth, int newHeigth)
        {
            return Scaling(Image.FromFile(filePath), newWidth, newHeigth);
        }

        /// <summary>
        /// 缩放图片
        /// </summary>
        /// <param name="sourceImage">原图片对象</param>
        /// <param name="newWidth">新宽度</param>
        /// <param name="newHeigth">新高度</param>
        /// <returns>缩放之后的图片对象</returns>
        public static Image Scaling(Image sourceImage, int newWidth, int newHeigth)
        {
            using (sourceImage)
            {
                //原图宽度和高度 
                int width = sourceImage.Width;
                int height = sourceImage.Height;
                int smallWidth;
                int smallHeight;
                //获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽 和 原图的高/缩略图的高) 
                if (((decimal)width) / height <= ((decimal)newWidth) / newHeigth)
                {
                    smallWidth = newWidth;
                    smallHeight = newWidth * height / width;
                }
                else
                {
                    smallWidth = newHeigth * width / height;
                    smallHeight = newHeigth;
                }
                //新建一个图板,以最小等比例压缩大小绘制原图 
                using (Image bitmap = new Bitmap(smallWidth, smallHeight))
                {
                    //绘制中间图 
                    using (Graphics g = Graphics.FromImage(bitmap))
                    {
                        //高清,平滑 
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        g.Clear(Color.Black);
                        g.DrawImage(sourceImage, new Rectangle(0, 0, smallWidth, smallHeight), new Rectangle(0, 0, width, height), GraphicsUnit.Pixel);
                    }

                    Image bitmap1 = new Bitmap(newWidth, newHeigth);
                    //绘制缩略图 
                    using (Graphics g = Graphics.FromImage(bitmap1))
                    {
                        //高清,平滑 
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        g.Clear(Color.Black);
                        int lwidth = (smallWidth - newWidth) / 2;
                        int bheight = (smallHeight - newHeigth) / 2;
                        g.DrawImage(bitmap, new Rectangle(0, 0, newWidth, newHeigth), lwidth, bheight, newWidth, newHeigth, GraphicsUnit.Pixel);
                        g.Dispose();

                        return bitmap1;
                    }
                }
            }
        }
    }
}